# -*- coding: utf-8 -*-
'''eof
name:申请人准贷记卡授信总额
code:IC99_IndQuaCreditCardAmt
tableName:
columnName:
groups:行内二代个人征信数据
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:
description:
eof'''

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0\
            or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def get_amount(table,col):
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    if table in [ 0,[{}]]:
        return 0
    else:
        if isfloat(table[0].get(col))==True and table[0].get(col) not in null_type_list:
            return float(table[0].get(col))
        else:
            return 0


def IC99_IndQuaCreditCardAmt():
    data = IC_DSJ_2D['data']
    try:
        table1=getTable(data,"PCO",1,0)#单张表
        if table1 == 0:
            return 0
        elif table1==[{}]:
            return u"缺失值"
        else:
            table3=getTable(table1[0],"PC02",0,0)
            if table3 == 0:
                return 0
            elif table3==[{}]:
                return u"缺失值"
            else:
                #准贷记卡账户汇总信息段-单张
                table4e=getTable(table3[0],"PC02I",0,1)
                if table4e in [[{}]]:
                    return u"缺失值"
                elif table4e == 0:
                    return 0
                else:
                    if not isfloat(table4e[0].get('PC02IJ01')):
                        return u"缺失值"
                    else:
                        factor = get_amount(table4e,'PC02IJ01')
                        return round(factor,4)
    except:
        return u"缺失值"

result = IC99_IndQuaCreditCardAmt()